﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Web.UI.Controls;
using DP.Common;

namespace CRM.Web.FaqManage
{
    public partial class FaqContentManageRight : Common.PageBase
    {
        T_FAQ_SRC_BLL sBll = new T_FAQ_SRC_BLL();
        T_FAQ_TREE_BLL tBll = new T_FAQ_TREE_BLL();

        private UserLoginInfo _loginInfo;
        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            UserLoginInfo.CheckLoginInfo();
            _loginInfo = UserLoginInfo.GetLoginInfo();
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pageDataInit();
            }
        }

        private void pageDataInit()
        {
            T_FAQ_SRC_Entity entity = new T_FAQ_SRC_Entity();
            entity = sBll.GetEntity("SRCID", PageHelper.GetQueryString(this.Page, "SRCID"));
            if (entity == null)
            {
                txtCATID.Text = PageHelper.GetQueryString(this.Page, "CATID");
            }
            else
            {
                PageHelper.SetControlValue(this.Page, entity);
                lblATTACHFILE.Text = GetFileName(entity.ATTACHFILE);
                fckNewsMessage.Value = entity.SRCCONTENT;
                this.lblTitle.Text = "FAQ内容编辑 >> " + PageHelper.GetQueryString(this.Page, "CATNAME");
            }
        }
        /// <summary>
        /// Handles the Click event of the btnUpload control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string filepath = UploadFile(this.Page, ImportFile);
            string filename = GetFileName(filepath);
            if (string.IsNullOrEmpty(filepath))
            {
                PageHelper.ShowMessage(this.Page, "文件上传失败！");
                return;
            }

            btnOK.Enabled = true;
            txtATTACHFILE.Text = StaticConstant.UploadFilePath + "\\" + filename;
            lblATTACHFILE.Text = GetFileName(ImportFile.PostedFile.FileName);
        }

        /// <summary>
        /// 文件 上传方法
        /// </summary>
        /// <param name="page">当前页面</param>
        /// <param name="auc">上传控件</param>
        /// <returns>上传后的文件的服务器路径</returns>
        private string UploadFile(Page page, System.Web.UI.WebControls.FileUpload auc)
        {
            string temp = DateTime.Now.ToString("yyyyMMddHHmmss");
            string filename = auc.PostedFile.FileName;
            filename = GetFileName(filename);

            string filepath = HttpContext.Current.Request.MapPath(StaticConstant.UploadFilePath);

            int index = filename.LastIndexOf('.');
            if (index != -1)
            {
                filename = filename.Insert(index, temp);
            }
            else
            {
                filename += temp;
            }

            string fullpath = string.Empty;
            if (filepath.LastIndexOf("\\") == filepath.Length - 1)
            {
                fullpath = filepath + filename;
            }
            else
            {
                fullpath = filepath + "\\" + filename;
            }

            try
            {
                auc.PostedFile.SaveAs(fullpath);
            }
            catch
            {
                fullpath = null;
            }
            return fullpath;
        }

        /// <summary>
        /// 在绝对路径中，获取Excel文件名
        /// </summary>
        /// <param name="input">需要导入的Excel绝对路径</param>
        /// <returns></returns>
        private string GetFileName(string input)
        {
            return input.Substring(input.LastIndexOf("\\") + 1);
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {

            PageHelper.ClearControlValue(this.Page);
            txtCATID.Text = PageHelper.GetQueryString(this.Page, "CATID");
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string strCatid = this.txtCATID.Text.Trim();
            string strSrcid = this.txtSRCID.Text.Trim();
            string strTitle = this.txtSRCTITLE.Text.Trim();

            if (string.IsNullOrEmpty(strTitle))
            {
                PageHelper.ShowMessage("发布主题不能为空！");
                return;
            }


            T_FAQ_SRC_Entity entity = new T_FAQ_SRC_Entity();

            entity = sBll.GetEntity("SRCID", strSrcid);
            try
            {
                if (entity == null)
                {
                    #region 新增
                    entity = new T_FAQ_SRC_Entity();
                    PageHelper.GetControlValue(this.Page, ref entity);
                    entity.BUSINESSDIVISIONCODE = _loginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    entity.DEPARTMENTCODE = _loginInfo.UserEntity.DEPARTMENTCODE;
                    entity.USERID = _loginInfo.UserEntity.USERID;
                    entity.CREATEDATE = DateTime.Now;
                    entity.SRCCONTENT = fckNewsMessage.Value.Trim();
                    sBll.Insert(entity);
                    #endregion
                }
                else
                {
                    #region 修改
                    PageHelper.GetControlValue(this.Page, ref entity);
                    entity.SRCCONTENT = fckNewsMessage.Value.Trim();
                    sBll.Update(entity);
                    #endregion
                }
                PageHelper.ShowMessage(this.Page, "保存成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this.Page)));
            }
            catch (Exception ex)
            {
                PageHelper.ShowMessage(this.Page, ex.Message);
            }
            //PageHelper.ShowMessage("保存成功！");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            T_FAQ_SRC_Entity entity = new T_FAQ_SRC_Entity();
            entity.SRCID = StringHelper.ToInt(txtSRCID.Text);
            try
            {
                long rel = sBll.Delete(entity);
                if (rel > 0)
                {
                    PageHelper.ShowMessage(this.Page, "删除成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this.Page)));
                    //Response.Redirect(HttpContext.Current.Request.RawUrl);
                    //PageHelper.ShowMessage("删除成功！");
                }
            }
            catch (Exception ex)
            {
                PageHelper.ShowMessage(this.Page, ex.Message);
            }
        }
    }
}
